package com.hiwhitley.easy.day20;

import com.hiwhitley.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hiwhitley on 2016/9/19.
 */
public class BinaryTreePaths_257 {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null) return res;
        StringBuilder sb = new StringBuilder();
        rec(root, res, sb);
        return res;
    }

    private void rec(TreeNode root, List<String> res, StringBuilder sb) {
        if(root.left==null && root.right==null) {
            sb.append(root.val);
            res.add(sb.toString());
            return;
        }

        sb.append(root.val);
        sb.append("->");
        int oriLen = sb.length();

        if(root.left!=null) rec(root.left, res, sb);
        sb.setLength(oriLen);
        if(root.right!=null) rec(root.right, res, sb);

    }
}
